package com.hl.card.award.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hl.card.award.mq.Mq4NotifyDemo;
import com.hl.card.common.persistence.dao.ZoneUserAwardMapper;
import com.hl.card.common.persistence.dao.ZoneUserAwardThreeMapper;
import com.hl.card.common.persistence.model.*;
import com.hl.card.common.util.DateUtils;
import com.hl.card.modular.pack.dao.DisMemberApplyMapper;
import com.hl.card.modular.pack.model.AwardDataModel;
import com.hl.card.modular.pack.service.DisMemberInfoOenService;
import com.hl.card.modular.pack.service.UserDataService;
import com.hl.card.modular.packBack.service.GoodsSevice;
import com.hl.card.modular.packBack.service.ISalesDivisionService;
import com.hl.card.award.service.IRewardByTrans;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.common.util.PayUtils;
import com.hl.card.modular.system.service.ISysDicService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * java类简单作用描述
 *
 * @ProjectName: distdev
 * @Package: com.hl.card.modular.pack.service.impl
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 作者姓名
 * @CreateDate: 2018/7/9 17:41
 * @UpdateUser: Neil.Zhou
 * @UpdateDate: 2018/7/9 17:41
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * <p>Copyright: Copyright (c) 2018</p>
 */
@Service
public class RewardByTransImpl implements IRewardByTrans {

    private static final Logger logger = LoggerFactory.getLogger(RewardByTransImpl.class);
    @Autowired
    ISysDicService sysDicService;

    @Autowired
    private ZoneUserAwardMapper zoneUserAwardMapper;

    @Autowired
    private ZoneUserAwardThreeMapper zoneUserAwardThreeMapper;

    @Autowired
    private PayUtils payUtils;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserDataService userDataService;


    @Autowired
    private DisMemberInfoOenService disMemberInfoOenService;


    @Autowired
    ISalesDivisionService iSalesDivisionService;

    @Autowired
    Mq4NotifyDemo mq4NotifyDemo;
    @Resource
    private GoodsSevice goodsSevice;

    @Resource(name = "disMemberApplyYMapper")
    private DisMemberApplyMapper disMemberApplyMapper;

    public ResponseObject manualReward(String userId, String parentCode, String prodCode, String amount, int count) {
        System.out.println("manualReward：人工发放奖励");
        return makeDistProfitSplit(userId, parentCode, prodCode, amount, count);
    }

    public ResponseObject manualReward(String userId, String prodCode, String amount, int count) {
        return makeDistProfitSplit(userId, prodCode, prodCode, amount, count);
    }

    public ResponseObject manualReward(String userId, String prodCode, String amount) {
        return makeDistProfitSplit(userId, prodCode, amount, true);
    }

    public ResponseObject manualReward(String userId, String prodCode) {
        return makeDistProfitSplit(userId, prodCode, "0", true);
    }

    public ResponseObject reward(String userId, String parentCode, String prodCode, String amount) {
        return makeDistProfitSplitOnlyOnetime(userId, parentCode, prodCode, amount);
    }

    public ResponseObject reward(String userId, String prodCode, String amount) {
        return reward(userId, prodCode, prodCode, amount);
    }

    public ResponseObject reward(String userId, String prodCode) {
        return makeDistProfitSplitOnlyOnetime(userId, prodCode, "0");
    }

    public ResponseObject checkReward(String userId, String parentCode, String prodCode) {
        return getAwardStatue(userId, parentCode, prodCode);
    }

    public ResponseObject checkReward(String userId, String prodCode) {
        return checkReward(userId, prodCode, prodCode);
    }

    public String getCurrCode(String parentCode) {
        String code = null;
        //code =  stringRedisTemplate.opsForValue().get("CURR_"+ parentCode + "_CODE");
        //System.out.println("getCurrCode: parentCode = " + parentCode + ";  prodCode = " + code);
        //if(StringUtils.isBlank(code)) {
        Goods goods = goodsSevice.selectByCode(parentCode);
        if(goods != null) {
            //stringRedisTemplate.opsForValue().set("CURR_"+ parentCode + "_CODE", goods.getNowtype());
            return goods.getNowtype();
        }
        //}
        return code;
    }

    public String getAwardKey(String userId, String prodCode) {
        String awardKey = userId + prodCode;
        return awardKey;
    }
    public String getAwardStatueKey(String userId, String prodCode) {
        String awardStatueKey = "statue" + userId + prodCode;
        return awardStatueKey;
    }

    public boolean checkAwardCanBeAccessed(String userId, String prodCode) {
        String awardKey = getAwardKey(userId, prodCode);
        Long ms = System.currentTimeMillis();
        String timeKey = Long.toString(ms/1000);
        //幂等校验
        String order = stringRedisTemplate.opsForValue().get(awardKey + timeKey);
        if(StringUtils.isBlank(order)) {
            stringRedisTemplate.opsForValue().set(awardKey + timeKey, "1", 1, TimeUnit.SECONDS);
            return true;
        }else {
            return false;
        }
    }

    public ResponseObject getAwardStatue(String userId, String parentCode, String prodCode) {
        ResponseObject res = new ResponseObject();
        Map map = new HashMap<>();
        if(StringUtils.isBlank(prodCode)) {
            map.put("code", "6");
            map.put("prodName", "undefined");
            map.put("count", 0);
            map.put("statue", "0");
            map.put("msg", "产品编码为空");
            System.out.println("产品编码为空");
            res.setData(map);
            res.setErroCode(1);
            return res;
        }

        if(StringUtils.isBlank(userId)) {
            map.put("code", "6");
            map.put("prodName", "undefined");
            map.put("count", 0);
            map.put("statue", "0");
            map.put("msg", "用户ID为空");
            System.out.println("用户ID为空");
            res.setData(map);
            res.setErroCode(1);
            return res;
        }

        String awardKey = getAwardKey(userId, prodCode);
        String awardStatueKey = getAwardStatueKey(userId, prodCode);

        //Map<String,String> rest = (Map<String, String>) userDataService.joinRebate(prodCode);
//		String repeatCount = rest.get("count");

//        if(!checkAwardCanBeAccessed(userId, prodCode)) {
//            map.put("code", "5");
//            map.put("prodName", "undefined");
//            map.put("count", 0);
//            map.put("statue", "0");
//            map.put("msg", "请勿频繁操作");
//            System.out.println(prodCode + "访问过于频繁");
//            res.setData(map);
//            res.setErroCode(1);
//            return res;
//        }

        Goods goods = goodsSevice.selectByCode(parentCode);
        if(goods == null) {
            map.put("code", "4");
            map.put("prodName", "undefined");
            map.put("count", 0);
            map.put("statue", "0");
            map.put("msg", "该奖励在系统中不存在");
            System.out.println(prodCode + "奖励在系统中不存在");
            res.setData(map);
            res.setErroCode(1);
            return res;
        }
        String prodName = goods.getGoodsName();
        if(StringUtils.isBlank(prodName)) {
            map.put("code", "4");
            map.put("prodName", prodName);
            map.put("count", 0);
            map.put("statue", "0");
            map.put("msg", "该奖励在系统中不存在");
            System.out.println(prodName + "奖励在系统中不存在");
            res.setData(map);
            res.setErroCode(1);
            return res;
        }

        String value = stringRedisTemplate.opsForValue().get(awardKey);
        String statue = stringRedisTemplate.opsForValue().get(awardStatueKey);
        int count = 0;
        if(StringUtils.isNotBlank(value) && !"null".equals(value)) {
            try {
                count = Integer.parseInt(value);
                map.put("count", count);
            } catch (NumberFormatException e) {
                map.put("count", 0);
                map.put("info", value);
            }
        }
        System.out.println("领取《" + prodName + "》奖励次数：value = " + value);
        if(StringUtils.isNotBlank(statue) && !"null".equals(statue)) {
            map.put("code", "1");
            map.put("prodName", prodName);
            map.put("statue", statue);
            map.put("msg", "已经领取奖励");
            System.out.println("已领取《" + prodName + "》奖励");
            res.setData(map);
            res.setErroCode(1);
        }else {
            map.put("code", "0");
            map.put("prodName", prodName);
            map.put("count", count);
            map.put("statue", "0");
            map.put("msg", "未领取奖励");
            System.out.println("未领取《" + prodName + "》奖励");
            res.setData(map);
            res.setErroCode(0);
        }
        return res;
    }
    public ResponseObject getAwardStatue(String userId, String prodCode) {
        return getAwardStatue(userId, prodCode, prodCode);
    }
    public ResponseObject makeDistProfitSplitOnlyOnetime(String userId, String parentCode, String prodCode, String amount) {
        System.out.println("makeDistProfitSplitOnlyOnetime->parentCode:" + parentCode);
        String awardKey = getAwardKey(userId, prodCode);
        String awardStatueKey = getAwardStatueKey(userId, prodCode);
        ResponseObject res = getAwardStatue(userId, parentCode, prodCode);
        int errorCode = res.getErroCode();
        Map map = (Map) res.getData();
        String prodName = (String) map.get("prodName");
        int count = (int) map.get("count");
        String statue = (String) map.get("statue");
        if(errorCode != 0) {
            return res;
        }
        res = makeDistProfitSplitOneTime(userId, parentCode, prodCode, amount);
        errorCode = res.getErroCode();
        map.put("code", "1");
        map.put("count", count);
        map.put("msg", "领取奖励失败");
        if(errorCode == 0) {//发放奖励成功
            map = (Map) res.getData();
            String code = (String) map.get("code");
            System.out.println("比豆奖励返回结果：" + (String) map.get("msg"));
            if("0".equals(code)){//处理成功
                stringRedisTemplate.opsForValue().set(awardKey, Integer.toString(count+1));
                stringRedisTemplate.opsForValue().set(awardStatueKey, "1");
                map.put("code", "0");
                map.put("count", count);
                map.put("msg", "领取" + prodName + "奖励成功");
                System.out.println("领取" + prodName + "奖励成功");
                res.setErroCode(0);
                return res;
            }
        }
        System.out.println("领取" + prodName + "奖励失败");
        res.setData(map);
        return res;
    }

    public ResponseObject makeDistProfitSplitOnlyOnetime(String userId, String prodCode, String amount) {
        return makeDistProfitSplitOnlyOnetime(userId, prodCode, prodCode, amount);
    }

    public ResponseObject makeDistProfitSplit(String userId, String parentCode, String prodCode, String amount, int count) {
        String mchOrderNo;
        mchOrderNo = prodCode + System.currentTimeMillis() + userId;
        System.out.println("makeDistProfitSplit->parentCode:" + parentCode);
        return makeDistProfitSplit(userId, parentCode, prodCode, amount, "bd", "AWARD", count, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplit(String userId, String parentCode, String prodCode, String amount, Boolean repeate) {
        String mchOrderNo;
        if(repeate) {
            mchOrderNo = prodCode + System.currentTimeMillis() + userId;
        }else {
            mchOrderNo = prodCode + userId;
        }
        System.out.println("makeDistProfitSplit->parentCode:" + parentCode);
        return makeDistProfitSplit(userId, parentCode, prodCode, amount, "bd", "AWARD", 0, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplit(String userId, String prodCode, String amount, Boolean repeate) {
        return makeDistProfitSplit(userId, prodCode, prodCode, amount, repeate);
    }

    public ResponseObject makeDistProfitSplitByCount(String userId, String prodCode, String amount, int count) {
        String mchOrderNo;
        mchOrderNo = prodCode + System.currentTimeMillis() + userId;
        return makeDistProfitSplitByCount(userId, prodCode, amount, "bd", "PAY", count, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplitByCountOneTime(String userId, String prodCode, String amount, int count) {
        String mchOrderNo;
        mchOrderNo = prodCode + userId;
        return makeDistProfitSplitByCount(userId, prodCode, amount, "bd", "AWARD", 0, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplitOneDayOneTime(String userId, String prodCode, String score) {
        String awardKey = getAwardKey(userId, prodCode);
        String awardStatueKey = getAwardStatueKey(userId, prodCode);
        ResponseObject res = getAwardStatue(userId, prodCode);

        int errorCode = res.getErroCode();
        if(errorCode != 0) {
            return res;
        }
        Map map = (Map) res.getData();
        String prodName = (String) map.get("prodName");
        int count = (int) map.get("count");
        String statue = (String) map.get("statue");

        String mchOrderNo;
        Date currDay;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        currDay = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

        String dateString = formatter.format(currDay);
        System.out.println("当天签到日期 = " + dateString);
        mchOrderNo = prodCode + dateString + userId;
        res = makeDistProfitSplit(userId, prodCode, score, "bd", "AWARD", 1, mchOrderNo);

        errorCode = res.getErroCode();
        map.put("code", "1");
        map.put("count", count);
        map.put("msg", "领取奖励失败");
        if(errorCode == 0) {//发放奖励成功
            map = (Map) res.getData();
            String code = (String) map.get("code");
            System.out.println("比豆奖励返回结果：" + (String) map.get("msg"));
            if("0".equals(code)){//处理成功
                stringRedisTemplate.opsForValue().set(awardKey, Integer.toString(count+1));
                stringRedisTemplate.opsForValue().set(awardStatueKey, "1");
                map.put("code", "0");
                map.put("count", count);
                map.put("msg", "领取" + prodName + "奖励成功");
                System.out.println("领取" + prodName + "奖励成功");
                res.setErroCode(0);
                return res;
            }
        }
        System.out.println("领取" + prodName + "奖励失败");
        res.setData(map);
        return res;


    }

    public ResponseObject makeDistProfitSplit(String userId, String parentCode, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo) {
        return makeDistProfitSplitByCount(userId, parentCode, prodCode, amount, "bd", "AWARD", count, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplit(String userId, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo) {
        return makeDistProfitSplit(userId, prodCode, prodCode, amount, "bd", "AWARD", count, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplitByCount(String userId, String parentCode, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo) {
        System.out.println("makeDistProfitSplitByCount->parentCode:" + parentCode);
        return makeDistProfitSplitByTrans(userId, parentCode, prodCode, amount, amountUnit, orderType, count, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplitByCount(String userId, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo) {
        return makeDistProfitSplitByCount(userId, prodCode, prodCode, amount, amountUnit, orderType, count, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplitByCount(String userId, String prodCode, String amount, int count, String mchOrderNo) {
        return makeDistProfitSplitByTrans(userId, prodCode, amount, "bd", "PAY", count, mchOrderNo);
    }

    public ResponseObject makeDistProfitSplitOneTime(String userId, String parentCode, String prodCode, String score) {
        return makeDistProfitSplit(userId, parentCode, prodCode, score, false);
    }

    public ResponseObject makeDistProfitSplitOneTime(String userId, String prodCode, String score) {
        return makeDistProfitSplitOneTime(userId, prodCode, prodCode, score);
    }

    public AwardDataModel getAwardData(String userId, String parentCode, String prodCode, String amount, int count, String orderType, String amountUnit) {
        String parentUserId = null;
        String parentPhone = null;
        AwardDataModel awardData = new AwardDataModel();
        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("getAwardData->parentCode=" + parentCode);
        System.out.println("count=" + count);
        if("fen".equals(amountUnit)){
            inputAmount = inputAmount/100;//转换为元
            amount = Long.toString(inputAmount);
        }else if("htl".equals(amountUnit)){
            //TODO HTL需要转换
        }

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", userId);
        User newUser = userDataService.selectOne(wrapper1);
        Wrapper<DisMemberInfoOen> wrapper = new EntityWrapper<>();
        wrapper.eq("dis_user_id", userId);
        DisMemberInfoOen disMemberInfo = disMemberInfoOenService.selectOne(wrapper);

        if (StringUtils.isBlank(userId)) {
            System.out.println("用户ID为空");
            awardData.setCode("6");
            awardData.setMsg("用户ID为空");
            return awardData;
        }

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            return awardData;
        }

//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer bibi;//bibi
        Map<String,Integer> re = (Map<String,Integer>) userDataService.fraction(parentCode, Double.valueOf(inputAmount));
        member=re.get("member");//huiyuang
        partner=re.get("partner");//hehuoren
        bibi=re.get("bibi");//bibi

        if(count > 1) {
            member = member*count;//huiyuang
            partner = partner*count;//hehuoren
            bibi = bibi*count;//bibi
        }
        Integer amountInt = member + partner;
        //String total = Double.toString(amountInt.doubleValue());
        String total = Integer.toString(amountInt);
//		Map<String,String> rest = (Map<String, String>) userDataService.joinRebate(productType);
//		String count = rest.get("count");
        System.out.println("普通会员：" + member);
        System.out.println("推荐人：" + partner);
        System.out.println("比比旅行：" + bibi);
        Goods goods = goodsSevice.selectByCode(parentCode);
        String prodName = goods.getGoodsName();
        if(StringUtils.isBlank(prodName)) {
            System.out.println(prodName + "奖励在系统中不存在");
            awardData.setCode("4");
            awardData.setMsg("该奖励在系统中不存在");
            return awardData;
        }
        JSONArray userList = new JSONArray();

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", prodName + ":" + prodCode);
        userList.add(user);

        //成为合伙人奖励的推荐人不再是默认的注册推人，而是合伙人推荐人
        DisMemberInfoOen parentDisMemberInfo = null;
        if(prodName.contains("合伙人")) {
            String dis_recommendation_no = disMemberApplyMapper.getDismemberCodeByUserId(userId);
            System.out.println("合伙人上级推荐人代码：" + dis_recommendation_no);
            if(StringUtils.isNotBlank(dis_recommendation_no)) {//有推荐上级
                //String dis_recomm_pcode = disMemberInfo.getDisRecommPcode();

                if(StringUtils.isNotBlank(dis_recommendation_no)) {
                    System.out.println("合伙人上级推荐人代码：" + dis_recommendation_no);
                    Wrapper<DisMemberInfoOen> wrapperp = new EntityWrapper<>();
                    wrapperp.eq("dis_recommendation_no", dis_recommendation_no);
                    //parentDisMemberInfo = disMemberInfoOenMapper.queryDisMemberInfoByCode(dis_recommendation_no);
                    parentDisMemberInfo = disMemberInfoOenService.selectOne(wrapperp);
                    if(parentDisMemberInfo != null) {
                        System.out.println("合伙人上级推荐人：" + parentDisMemberInfo.getDisUserName());
                        if(partner == 0) {
                            parentUserId = null;
                            parentPhone = null;
                        }else {
                            parentUserId = parentDisMemberInfo.getDisUserId();
                            parentPhone = parentDisMemberInfo.getBindinPhone();
                            user = new JSONObject();
                            user.put("userId", parentUserId);
                            user.put("phone", parentPhone);
                            user.put("amount", partner);
                            user.put("comment", prodName + "-推荐人奖励:" + prodCode + "-T");
                            userList.add(user);
                            //二级奖励
                            re = (Map<String,Integer>) userDataService.fraction("SJTJJL", Double.valueOf(inputAmount));
                            member=re.get("member");//huiyuang
                            partner=re.get("partner");//hehuoren
                            bibi=re.get("bibi");//bibi
                            if(count > 1) {
                                member = member*count;//huiyuang
                                partner = partner*count;//hehuoren
                                bibi = bibi*count;//bibi
                            }
                            amountInt += member + partner;
                            //String total = Double.toString(amountInt.doubleValue());
                            total = Integer.toString(amountInt);
//		Map<String,String> rest = (Map<String, String>) userDataService.joinRebate(productType);
//		String count = rest.get("count");
                            System.out.println("推荐人本人：" + member);
                            System.out.println("上级推荐人：" + partner);
                            System.out.println("比比旅行：" + bibi);
                            goods = goodsSevice.selectByCode("SJTJJL");
                            prodName = goods.getGoodsName();
                            if(StringUtils.isBlank(prodName)) {
                                System.out.println(prodName + "奖励在系统中不存在");
                                awardData.setCode("4");
                                awardData.setMsg("该奖励在系统中不存在");
                                return awardData;
                            }
                            dis_recommendation_no = parentDisMemberInfo.getDisRecommPcode();
                            if(StringUtils.isNotBlank(dis_recommendation_no)) {//有推荐上级
                                //String dis_recomm_pcode = disMemberInfo.getDisRecommPcode();

                                if(StringUtils.isNotBlank(dis_recommendation_no)) {
                                    System.out.println("上级推荐人代码：" + dis_recommendation_no);
                                    wrapperp = new EntityWrapper<>();
                                    wrapperp.eq("dis_recommendation_no", dis_recommendation_no);
                                    //parentDisMemberInfo = disMemberInfoOenMapper.queryDisMemberInfoByCode(dis_recommendation_no);
                                    parentDisMemberInfo = disMemberInfoOenService.selectOne(wrapperp);
                                    if(parentDisMemberInfo != null) {
                                        System.out.println("上级推荐人：" + parentDisMemberInfo.getDisUserName());
                                        parentUserId = parentDisMemberInfo.getDisUserId();
                                        parentPhone = parentDisMemberInfo.getBindinPhone();
                                        user = new JSONObject();
                                        user.put("userId", parentUserId);
                                        user.put("phone", parentPhone);
                                        user.put("amount", member);
                                        user.put("comment", prodName + "-推荐人奖励:" + prodCode + "-T");
                                        userList.add(user);
                                    }
                                }else {
                                    System.out.println("该用户无上级推荐人");
                                }
                            }
                        }
                    }
                }else {
                    System.out.println("该用户无上级推荐人");
                }
            }
        }else {
            //用户验证码+
            String regCode=newUser.getReg_code();
            //上级用户推荐码
            String referCode=newUser.getRefer_reg_code();

            if (disMemberInfo == null) {
                System.out.println("普通会员");
                if(StringUtils.isNotBlank(referCode)) {//有上级
                    System.out.println("注册推荐人");
//                    int referCodeCount;
//                    String countStr = stringRedisTemplate.opsForValue().get(referCode);
//                    if(StringUtils.isBlank(countStr)) {
//                        countStr = "0";
//                    }
//                    referCodeCount = Integer.parseInt(countStr);
//                    List<Map<String, Object>> list = sysDicService.selectListByCode("YQZCXZ");
//                    int max = -1;
//                    if(list != null) {
//                        String value = (String) list.get(0).get("MAX");
//                        if(StringUtils.isNotBlank(value)) {
//                            try {
//                                max = Integer.parseInt(value);
//                            } catch (NumberFormatException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }

                    if (userDataService.checkRegCodeUsedLimited(referCode)) {
                        System.out.println("推荐人奖励已达上限");
                    } else {
                        Wrapper<User> wrapperuser = new EntityWrapper<User>();
                        wrapperuser.eq("reg_code", referCode);
                        User parentUser = userDataService.selectOne(wrapperuser);
                        Wrapper<DisMemberInfoOen> wrapperp = new EntityWrapper<>();
                        wrapperp.eq("dis_user_id", parentUser.getUserId());
                        parentDisMemberInfo = disMemberInfoOenService.selectOne(wrapperp);
                        //如果推荐人不是合伙人，就不能奖励给推荐人
                        if (parentDisMemberInfo != null || "AWARD".equals(orderType)) {
                            if (partner == 0) {
                                parentUserId = null;
                                parentPhone = null;
                            } else {
                                parentUserId = parentUser.getUserId();
                                parentPhone = parentUser.getPhone();
                                user = new JSONObject();
                                user.put("userId", parentUserId);
                                user.put("phone", parentPhone);
                                user.put("amount", partner);
                                user.put("comment", prodName + "-推荐人奖励:" + prodCode + "-T");
                                userList.add(user);
                            }
                        }
                        System.out.println("注册推荐人手机号：" + parentUser.getPhone());

//                        referCodeCount ++;
//                        stringRedisTemplate.opsForValue().set(referCode, Integer.toString(referCodeCount));
                    }
                }
            } else {
                System.out.println("合伙人");
                if(partner == 0) {
                    parentUserId = null;
                    parentPhone = null;
                }else {
                    parentUserId = userId;
                    parentPhone = newUser.getPhone();
                    user = new JSONObject();
                    user.put("userId", parentUserId);
                    user.put("phone", parentPhone);
                    user.put("amount", partner);
                    user.put("comment", prodName + "-推荐人奖励:" + prodCode + "-T");
                    userList.add(user);
                }
            }
        }

        String prefit = "";
        if("PAY".equals(orderType)) {//转比豆到用户账号
            awardData.setOrderType("1");
            prefit = "支付金额：";
            awardData.setUnfreezeStringTime(null);
            awardData.setChannelId("HL_AWARD_HTL");//购买商品奖励
            awardData.setCurrency("htl");
        }else if("PAY_REFUND".equals(orderType)){//退款
            awardData.setOrderType("2");
            prefit = "退款金额：";
            awardData.setUnfreezeStringTime(null);
            awardData.setChannelId("HL_AWARD_HTL");//原路返回
            awardData.setCurrency("htl");
        }else if("AWARD_REFUND".equals(orderType)){//退款
            awardData.setOrderType("2");
            prefit = "退款金额：";
            awardData.setUnfreezeStringTime(null);
            awardData.setChannelId("HL_AWARD");//原路返回
            awardData.setCurrency("bd");
        }else if("AWARD".equals(orderType)){//空投奖励，推广奖励
            awardData.setOrderType("1");
            prefit = "比豆直接奖励：";
            amount = total;
            awardData.setUnfreezeStringTime(null);
            if(prodCode.contains("CPTY")) {//发放临时奖励
                awardData.setChannelId("HL_AWARD_HTL");//空投奖励
                awardData.setCurrency("htl");
            }else {
                awardData.setChannelId("HL_AWARD");//空投奖励
                awardData.setCurrency("bd");
            }
        }else {
            System.out.println(prodName + "不支持的奖励订单类型");
            awardData.setCode("9");
            awardData.setMsg(prodName + "不支持的奖励订单类型");
            return awardData;
        }

        if("bd".equals(amountUnit)) {
            awardData.setOrderDetail(prefit + amount+" 比豆");//原始订单金额,单位：比豆
        }else if("yuan".equals(amountUnit)){
            awardData.setOrderDetail(prefit + amount+" 元");//原始订单金额,单位：元
        }else if("fen".equals(amountUnit)){
            awardData.setOrderDetail(prefit + amount+" 分");//原始订单金额,单位：分
        }else if("htl".equals(amountUnit)){
            awardData.setOrderDetail(prefit + amount+" htl");//原始订单金额,单位：HTL
        }else {
            System.out.println(prodName + "订单金额的计量单位不支持");
            awardData.setCode("8");
            awardData.setMsg("prodName + \"订单金额的计量单位不支持\"");
            return awardData;
        }

        awardData.setOrderAmount(amount);
        awardData.setCode("0");
        awardData.setMemberAward(Integer.toString(member));
        awardData.setParentAward(Integer.toString(partner));
        awardData.setTotalAward(total);//总奖励数量，单位比豆
        awardData.setProdCode(prodCode);
        awardData.setProdName(prodName);
        awardData.setParentProdCode(prodCode + "-T");
        awardData.setParentProdName(prodName + "-推荐人奖励");
        awardData.setUserId(userId);
        awardData.setParentUserId(parentUserId);
        awardData.setParentPhone(parentPhone);
        awardData.setMsg("获取奖励数据成功");
        awardData.setUser(newUser);
        awardData.setDisMemberInfo(parentDisMemberInfo);
        awardData.setSettleList(userList.toJSONString());
        return awardData;
    }

    public AwardDataModel getAwardData(String userId, String prodCode, String amount, int count, String orderType, String amountUnit) {
        return getAwardData(userId, prodCode, prodCode, amount, count, orderType, amountUnit);
    }

    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject makeDistProfitSplitByTrans(String userId, String parentCode, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo) {
        return makeDistProfitSplitByTrans(userId, parentCode, prodCode, amount, amountUnit, orderType, count, mchOrderNo, null);
    }


    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject makeDistProfitSplitByTrans(String userId, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo) {
        return makeDistProfitSplitByTrans(userId, prodCode, amount, amountUnit, orderType, count, mchOrderNo, null);
    }

    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject makeDistProfitSplitByTrans(String userId, String parentCode, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo, String nnfreezeStringTime) {
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + parentCode);
        AwardDataModel awardData = getAwardData(userId, parentCode, prodCode, amount, count, orderType, amountUnit);
        Map map = new HashMap<>();
        ResponseObject res = new ResponseObject();
        if(!"0".equals(awardData.getCode())) {
            map.put("code", awardData.getCode());
            map.put("msg", awardData.getMsg());
            res.setData(map);
            res.setErroCode(1);
            return res;
        }

        JSONObject bidouTransOrder = new JSONObject();
        User user = awardData.getUser();
        DisMemberInfoOen disMemberInfo = awardData.getDisMemberInfo();
        int memberAward = Integer.parseInt(awardData.getMemberAward());
        int parentAward = Integer.parseInt(awardData.getParentAward());
        System.out.println("合伙人");
        System.out.println("merchantOrderNumber=" + mchOrderNo);
        bidouTransOrder.put("merchantOrderNumber", mchOrderNo);
        bidouTransOrder.put("loginAccount", user.getPhone());

        bidouTransOrder.put("dealType", awardData.getOrderType());
        bidouTransOrder.put("flag", awardData.getProdCode());
        bidouTransOrder.put("productType", awardData.getProdName());
        bidouTransOrder.put("parentFlag", awardData.getParentProdCode());
        bidouTransOrder.put("parentProductType", awardData.getParentProdName());
        bidouTransOrder.put("tripTime", nnfreezeStringTime);
        bidouTransOrder.put("orderAmount", awardData.getTotalAward());
        bidouTransOrder.put("dealDetail", awardData.getOrderDetail());
        bidouTransOrder.put("amount", awardData.getTotalAward());
//        bidouTransOrder.put("dealBringScoreType", "");
//        bidouTransOrder.put("dealBringScoreRate", "");
//        bidouTransOrder.put("dealProxyAccountRate", "");
        bidouTransOrder.put("scoreReturnAmount", memberAward+parentAward);
        bidouTransOrder.put("userAmount", awardData.getMemberAward());
        bidouTransOrder.put("partnerAmount", awardData.getParentAward());

        bidouTransOrder.put("userId", userId);
        bidouTransOrder.put("userPhone", user.getPhone());
        String parentUserId = awardData.getParentUserId();
        String parentPhone = awardData.getParentPhone();
        if(StringUtils.isNotBlank(parentUserId)) {
            bidouTransOrder.put("proxyNumber", parentUserId);
            bidouTransOrder.put("proxyPhone", parentPhone);
        }
        bidouTransOrder.put("channelId", awardData.getChannelId());
        bidouTransOrder.put("currency", awardData.getCurrency());
        bidouTransOrder.put("settleList", awardData.getSettleList());
        System.out.println("比豆奖励接口请求参数：" + bidouTransOrder.toJSONString());
        JSONObject result;
//        if("REFUND".equals(orderType)) {
//            result = payUtils.refundBidouAward(bidouTransOrder);
//        }else if("AWARD".equals(orderType) || "PAY".equals(orderType)){
        //result = payUtils.transBidouByOrder(userId, user.getPhone(), bidouTransOrder);
        String channelId = awardData.getChannelId();
        if("HL_AWARD_HTL".equals(channelId)) {
            result = payUtils.hlawardByTrans(userId, user.getPhone(), bidouTransOrder);
        }else {
            result = payUtils.awardByTrans(userId, user.getPhone(), bidouTransOrder);
        }
//        }else {
//            result = "fail";
//        }
        System.out.println("支付中心返回 结果：" + result);
        //{"errCodeDes":"商户订单重复","errCode":"0119","retCode":"FAIL","retMsg":"订单重复"}
        if(result != null) {
            if("success".equalsIgnoreCase(result.getString("retCode"))) {
                map.put("code", "0");
                map.put("memberAward", memberAward);
                map.put("partnerAward", parentAward);
                map.put("msg", "领取奖励成功");
                res.setData(map);
                res.setErroCode(0);
            }else {
                if(result.getString("retMsg").contains("重复")) {
                    map.put("code", "10");
                    map.put("msg", "重复领取奖励");
                    res.setData(map);
                    res.setErroCode(1);
                }else {
                    map.put("code", "1");
                    map.put("msg", "领取奖励失败");
                    res.setData(map);
                    res.setErroCode(1);
                }
            }

        }else {
            map.put("code", "1");
            map.put("msg", "领取奖励失败");
            res.setData(map);
            res.setErroCode(1);
        }
        return res;
    }

    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject makeDistProfitSplitByTrans(String userId, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo, String nnfreezeStringTime) {
        return makeDistProfitSplitByTrans(userId, prodCode, prodCode, amount, amountUnit, orderType, count, mchOrderNo, nnfreezeStringTime);
    }


    //分配奖励，奖励金额将会被平均分成多份，余数加到第一单
    public ResponseObject makeDistProfitSplitByBatch(String userId, String prodCode, String amount, int count, String mchOrderNo) {
        return makeDistProfitSplitByBatch(userId, prodCode, prodCode, amount, "htl", "PAY", count, mchOrderNo);
    }

    //分配奖励，微信社区推广建设专用
    public ResponseObject makeDistProfitSplitByWxCommunity(String communityAdmin, String recommender, String userId, String amount) {
        return makeDistProfitSplitByWxCommunity(communityAdmin, recommender, userId, amount, "htl", 1);
    }
    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject doDistProfitSplit(AwardDataModel awardData, String userId, String parentCode, String mchOrderNo, String nnfreezeStringTime) {
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + parentCode);
        Map map = new HashMap<>();
        ResponseObject res = new ResponseObject();
        if(!"0".equals(awardData.getCode())) {
            map.put("code", awardData.getCode());
            map.put("msg", awardData.getMsg());
            res.setData(map);
            res.setErroCode(1);
            return res;
        }

        JSONObject bidouTransOrder = new JSONObject();
        User user = awardData.getUser();
        DisMemberInfoOen disMemberInfo = awardData.getDisMemberInfo();
        int memberAward = Integer.parseInt(awardData.getMemberAward());
        int parentAward = Integer.parseInt(awardData.getParentAward());
        System.out.println("合伙人");
        System.out.println("merchantOrderNumber=" + mchOrderNo);
        bidouTransOrder.put("merchantOrderNumber", mchOrderNo);
        bidouTransOrder.put("loginAccount", user.getPhone());

        bidouTransOrder.put("dealType", awardData.getOrderType());
        bidouTransOrder.put("flag", awardData.getProdCode());
        bidouTransOrder.put("productType", awardData.getProdName());
        bidouTransOrder.put("parentFlag", awardData.getParentProdCode());
        bidouTransOrder.put("parentProductType", awardData.getParentProdName());
        bidouTransOrder.put("tripTime", nnfreezeStringTime);
        bidouTransOrder.put("orderAmount", awardData.getTotalAward());
        bidouTransOrder.put("dealDetail", awardData.getOrderDetail());
        bidouTransOrder.put("amount", awardData.getTotalAward());
//        bidouTransOrder.put("dealBringScoreType", "");
//        bidouTransOrder.put("dealBringScoreRate", "");
//        bidouTransOrder.put("dealProxyAccountRate", "");
        bidouTransOrder.put("scoreReturnAmount", memberAward+parentAward);
        bidouTransOrder.put("userAmount", awardData.getMemberAward());
        bidouTransOrder.put("partnerAmount", awardData.getParentAward());

        bidouTransOrder.put("userId", userId);
        bidouTransOrder.put("userPhone", user.getPhone());
        String parentUserId = awardData.getParentUserId();
        String parentPhone = awardData.getParentPhone();
        if(StringUtils.isNotBlank(parentUserId)) {
            bidouTransOrder.put("proxyNumber", parentUserId);
            bidouTransOrder.put("proxyPhone", parentPhone);
        }
        bidouTransOrder.put("channelId", awardData.getChannelId());
        bidouTransOrder.put("currency", awardData.getCurrency());
        bidouTransOrder.put("settleList", awardData.getSettleList());
        System.out.println("比豆奖励接口请求参数：" + bidouTransOrder.toJSONString());
        JSONObject result;
//        if("REFUND".equals(orderType)) {
//            result = payUtils.refundBidouAward(bidouTransOrder);
//        }else if("AWARD".equals(orderType) || "PAY".equals(orderType)){
        //result = payUtils.transBidouByOrder(userId, user.getPhone(), bidouTransOrder);
        String channelId = awardData.getChannelId();
        if("HL_AWARD_HTL".equals(channelId)) {
            result = payUtils.hlawardByTrans(userId, user.getPhone(), bidouTransOrder);
        }else {
            result = payUtils.awardByTrans(userId, user.getPhone(), bidouTransOrder);
        }
//        }else {
//            result = "fail";
//        }
        System.out.println("支付中心返回 结果：" + result);
        //{"errCodeDes":"商户订单重复","errCode":"0119","retCode":"FAIL","retMsg":"订单重复"}
        map.put("mchOrderNo", mchOrderNo);
        if(result != null) {
            map.put("retMsg", result.getString("retMsg"));
            if("success".equalsIgnoreCase(result.getString("retCode"))) {
                map.put("code", "0");
                map.put("prodCode", awardData.getProdCode());
                map.put("memberAward", memberAward);
                map.put("partnerAward", parentAward);
                map.put("msg", "领取奖励成功");
                res.setData(map);
                res.setErroCode(0);
            }else {
                if(result.getString("retMsg").contains("重复")) {
                    map.put("code", "10");
                    map.put("msg", "重复领取奖励");
                    res.setData(map);
                    res.setErroCode(1);
                }else {
                    map.put("code", "1");
                    map.put("msg", "领取奖励失败");
                    res.setData(map);
                    res.setErroCode(1);
                }
            }

        }else {
            map.put("code", "11");
            map.put("msg", "支付中心无响应");
            res.setData(map);
            res.setErroCode(1);
        }
        return res;
    }

    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject makeDistProfitSplitByBatch(String userId, String parentCode, String prodCode, String amount, String amountUnit, String orderType, int count, String mchOrderNo) {
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + parentCode);
        AwardDataModel awardData = new AwardDataModel();
        {
            String parentUserId = null;
            String parentPhone = null;

            //User newUser = userDataService.selectById(user.getUserId());
            Long inputAmount = Long.parseLong(amount);
            int inputAmountInt = Integer.parseInt(amount);
            System.out.println("getAwardData->parentCode=" + parentCode);
            System.out.println("count=" + count);
            if("fen".equals(amountUnit)){
                inputAmount = inputAmount/100;//转换为元
                amount = Long.toString(inputAmount);
            }else if("htl".equals(amountUnit)){
                //TODO HTL需要转换
            }

            Wrapper<User> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("user_id", userId);
            User newUser = userDataService.selectOne(wrapper1);
            Wrapper<DisMemberInfoOen> wrapper = new EntityWrapper<>();
            wrapper.eq("dis_user_id", userId);
            DisMemberInfoOen disMemberInfo = disMemberInfoOenService.selectOne(wrapper);

//            if (StringUtils.isBlank(userId)) {
//                System.out.println("用户ID为空");
//                awardData.setCode("6");
//                awardData.setMsg("用户ID为空");
//            }
//
//            if (null == newUser) {
//                System.out.println("用户不存在");
//                awardData.setCode("2");
//                awardData.setMsg("用户不存在");
//            }

            ResponseObject result = new ResponseObject();
            Map map = new HashMap<>();
            if (StringUtils.isBlank(userId)) {
                System.out.println("用户ID为空");
                awardData.setCode("6");
                awardData.setMsg("用户ID为空");
                map.put("code", "6");
                map.put("msg", "用户ID为空");
                result.setData(map);
                result.setErroCode(1);
                return result;
            }

            if (null == newUser) {
                System.out.println("用户不存在");
                awardData.setCode("2");
                awardData.setMsg("用户不存在");
                map.put("code", "2");
                map.put("msg", "用户不存在");
                result.setData(map);
                result.setErroCode(1);
                return result;
            }

            Goods goods = goodsSevice.selectByCode(prodCode);
            if(goods == null) {
                map.put("code", "4");
                map.put("msg", "该奖励在系统中不存在");
                result.setData(map);
                result.setErroCode(1);
                return result;
            }
            String prodName = goods.getGoodsName();
            //parentProdName = parentProdName.replace("奖励", "");
            if(StringUtils.isBlank(prodName)) {
                System.out.println(prodName + "奖励在系统中不存在");
                map.put("code", "4");
                map.put("msg", "该奖励在系统中不存在");
                result.setData(map);
                result.setErroCode(1);
                return result;
            }

//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
            Integer member;//huiyuang
            Integer partner;//hehuoren
            Integer bibi;//bibi
            Map<String,Integer> re = (Map<String,Integer>) userDataService.fraction(parentCode, Double.valueOf(inputAmount));
            member=re.get("member");//huiyuang
            partner=re.get("partner");//hehuoren
            bibi=re.get("bibi");//bibi

//            if(count > 1) {
//                member = member*count;//huiyuang
//                partner = partner*count;//hehuoren
//                bibi = bibi*count;//bibi
//            }
            Integer amountInt = member + partner;
            //String total = Double.toString(amountInt.doubleValue());
            String total = Integer.toString(amountInt);
//		Map<String,String> rest = (Map<String, String>) userDataService.joinRebate(productType);
//		String count = rest.get("count");
            System.out.println("普通会员：" + member);
            System.out.println("推荐人：" + partner);
            System.out.println("比比旅行：" + bibi);
//            Goods goods = goodsSevice.selectByCode(parentCode);
//            String prodName = goods.getGoodsName();
//            if(StringUtils.isBlank(prodName)) {
//                System.out.println(prodName + "奖励在系统中不存在");
//                awardData.setCode("4");
//                awardData.setMsg("该奖励在系统中不存在");
//            }
            JSONArray userList = new JSONArray();

            JSONObject user = new JSONObject();
            user.put("userId", userId);
            user.put("phone", newUser.getPhone());
            user.put("amount", member);
            user.put("comment", prodName + ":" + prodCode);
            userList.add(user);

            String prefit = "";
            if("PAY".equals(orderType)) {//转比豆到用户账号
                awardData.setOrderType("1");
                prefit = "支付金额：";
                awardData.setUnfreezeStringTime(null);
                awardData.setChannelId("HL_AWARD_HTL");//购买商品奖励
                awardData.setCurrency("htl");
            }else if("PAY_REFUND".equals(orderType)){//退款
                awardData.setOrderType("2");
                prefit = "退款金额：";
                awardData.setUnfreezeStringTime(null);
                awardData.setChannelId("HL_AWARD_HTL");//原路返回
                awardData.setCurrency("htl");
            }else {
                System.out.println(prodName + "不支持的奖励订单类型");
                awardData.setCode("9");
                awardData.setMsg(prodName + "不支持的奖励订单类型");
            }

            if("bd".equals(amountUnit)) {
                awardData.setOrderDetail(prefit + amount+" 比豆");//原始订单金额,单位：比豆
            }else if("yuan".equals(amountUnit)){
                awardData.setOrderDetail(prefit + amount+" 元");//原始订单金额,单位：元
            }else if("fen".equals(amountUnit)){
                awardData.setOrderDetail(prefit + amount+" 分");//原始订单金额,单位：分
            }else if("htl".equals(amountUnit)){
                awardData.setOrderDetail(prefit + amount+" htl");//原始订单金额,单位：HTL
            }else {
                System.out.println(prodName + "订单金额的计量单位不支持");
                awardData.setCode("8");
                awardData.setMsg("prodName + \"订单金额的计量单位不支持\"");
            }

            awardData.setOrderAmount(amount);
            awardData.setCode("0");
            awardData.setMemberAward(Integer.toString(member));
            awardData.setParentAward(Integer.toString(partner));
            awardData.setTotalAward(total);//总奖励数量，单位比豆
            awardData.setProdCode(prodCode);
            awardData.setProdName(prodName);
            awardData.setParentProdCode(prodCode + "-T");
            awardData.setParentProdName(prodName + "-推荐人奖励");
            awardData.setUserId(userId);
            awardData.setParentUserId(parentUserId);
            awardData.setParentPhone(parentPhone);
            awardData.setMsg("获取奖励数据成功");
            awardData.setUser(newUser);
            awardData.setDisMemberInfo(null);
            awardData.setSettleList(userList.toJSONString());

            Date nextDay;
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.add(calendar.DATE,1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
            nextDay = calendar.getTime();
            //首期立即到账
            doDistProfitSplit(awardData, userId, parentCode, mchOrderNo + "Batch0", null);
            //剩余的按月发放
            for(int i = 1; i < count; i ++) {
                String unfreeze = DateUtils.DateToStr(nextDay, "yyyy-MM-dd HH:mm:ss");
                doDistProfitSplit(awardData, userId, parentCode, mchOrderNo + "Batch" + i, unfreeze);
                calendar.add(calendar.MONTH,1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
                nextDay = calendar.getTime();
            }

            userList = new JSONArray();
            amountInt = 0;
            String phone;
            //成为合伙人奖励的推荐人不再是默认的注册推人，而是合伙人推荐人
            DisMemberInfoOen parentDisMemberInfo = null;
            String dis_recommendation_no = newUser.getRefer_reg_code();
            System.out.println("合伙人上级推荐人代码：" + dis_recommendation_no);
            User parentUser;
            if(StringUtils.isNotBlank(dis_recommendation_no)) {//有推荐上级
                //String dis_recomm_pcode = disMemberInfo.getDisRecommPcode();

                if(StringUtils.isNotBlank(dis_recommendation_no)) {
                    System.out.println("合伙人上级推荐人代码：" + dis_recommendation_no);
                    Wrapper<User> wrapperuser = new EntityWrapper<User>();
                    wrapperuser.eq("reg_code", dis_recommendation_no);
                    newUser = userDataService.selectOne(wrapperuser);

                    if(newUser != null) {
                        System.out.println("合伙人上级推荐人：" + newUser.getName());
                        {
                            //上级奖励
                            re = (Map<String,Integer>) userDataService.fraction("TJHHR", Double.valueOf(inputAmount));
                            member=re.get("member");//huiyuang
                            partner=re.get("partner");//hehuoren
                            bibi=re.get("bibi");//bibi
//                            if(count > 1) {
//                                member = member*count;//huiyuang
//                                partner = partner*count;//hehuoren
//                                bibi = bibi*count;//bibi
//                            }
                            amountInt += member + partner;
                            //String total = Double.toString(amountInt.doubleValue());

//		Map<String,String> rest = (Map<String, String>) userDataService.joinRebate(productType);
//		String count = rest.get("count");
                            System.out.println("推荐人本人：" + member);
                            System.out.println("上级推荐人：" + partner);
                            System.out.println("比比旅行：" + bibi);

                            userId = newUser.getUserId();
                            phone = newUser.getPhone();
                            user = new JSONObject();
                            user.put("userId", userId);
                            user.put("phone", phone);
                            user.put("amount", member);
                            user.put("comment", prodName + "-推荐人奖励:" + prodCode + "-T");
                            userList.add(user);
                            //二级奖励
                            re = (Map<String,Integer>) userDataService.fraction("SJTJJL", Double.valueOf(inputAmount));
                            member=re.get("member");//huiyuang
                            partner=re.get("partner");//hehuoren
                            bibi=re.get("bibi");//bibi
//                            if(count > 1) {
//                                member = member*count;//huiyuang
//                                partner = partner*count;//hehuoren
//                                bibi = bibi*count;//bibi
//                            }
                            amountInt += member + partner;
                            //String total = Double.toString(amountInt.doubleValue());
                            total = Integer.toString(amountInt);
//		Map<String,String> rest = (Map<String, String>) userDataService.joinRebate(productType);
//		String count = rest.get("count");
                            System.out.println("推荐人本人：" + member);
                            System.out.println("上级推荐人：" + partner);
                            System.out.println("比比旅行：" + bibi);
                            goods = goodsSevice.selectByCode("SJTJJL");
                            prodName = goods.getGoodsName();
                            if(StringUtils.isBlank(prodName)) {
                                System.out.println(prodName + "奖励在系统中不存在");
                                awardData.setCode("4");
                                awardData.setMsg("该奖励在系统中不存在");
                            }
                            dis_recommendation_no = newUser.getRefer_reg_code();
                            if(StringUtils.isNotBlank(dis_recommendation_no)) {//有推荐上级

                                if(StringUtils.isNotBlank(dis_recommendation_no)) {
                                    System.out.println("合伙人上级推荐人代码：" + dis_recommendation_no);
                                    wrapperuser = new EntityWrapper<User>();
                                    wrapperuser.eq("reg_code", dis_recommendation_no);
                                    parentUser = userDataService.selectOne(wrapperuser);

                                    if(parentUser != null) {
                                        System.out.println("合伙人上级推荐人：" + parentUser.getName());
                                        if(partner == 0) {
                                            parentUserId = null;
                                            parentPhone = null;
                                        }else {
                                            parentUserId = parentUser.getUserId();
                                            parentPhone = parentUser.getPhone();
                                            user = new JSONObject();
                                            user.put("userId", parentUserId);
                                            user.put("phone", parentPhone);
                                            user.put("amount", member);
                                            user.put("comment", prodName + "-推荐人上级奖励:" + prodCode + "-T");
                                            userList.add(user);
                                        }
                                    }
                                }else {
                                    System.out.println("该用户无上级推荐人");
                                }
                            }
                        }
                    }
                }else {
                    System.out.println("该用户无上级推荐人");
                }
            }

            prefit = "";
            if("PAY".equals(orderType)) {//转比豆到用户账号
                awardData.setOrderType("1");
                prefit = "支付金额：";
                awardData.setUnfreezeStringTime(null);
                awardData.setChannelId("HL_AWARD_HTL");//购买商品奖励
                awardData.setCurrency("htl");
            }else if("PAY_REFUND".equals(orderType)){//退款
                awardData.setOrderType("2");
                prefit = "退款金额：";
                awardData.setUnfreezeStringTime(null);
                awardData.setChannelId("HL_AWARD_HTL");//原路返回
                awardData.setCurrency("htl");
            }else {
                System.out.println(prodName + "不支持的奖励订单类型");
                awardData.setCode("9");
                awardData.setMsg(prodName + "不支持的奖励订单类型");
            }

            if("bd".equals(amountUnit)) {
                awardData.setOrderDetail(prefit + amount+" 比豆");//原始订单金额,单位：比豆
            }else if("yuan".equals(amountUnit)){
                awardData.setOrderDetail(prefit + amount+" 元");//原始订单金额,单位：元
            }else if("fen".equals(amountUnit)){
                awardData.setOrderDetail(prefit + amount+" 分");//原始订单金额,单位：分
            }else if("htl".equals(amountUnit)){
                awardData.setOrderDetail(prefit + amount+" htl");//原始订单金额,单位：HTL
            }else {
                System.out.println(prodName + "订单金额的计量单位不支持");
                awardData.setCode("8");
                awardData.setMsg("prodName + \"订单金额的计量单位不支持\"");
            }

            awardData.setOrderAmount(total);
            awardData.setCode("0");
            awardData.setMemberAward(Integer.toString(member));
            awardData.setParentAward(Integer.toString(partner));
            awardData.setTotalAward(total);//总奖励数量，单位比豆
            awardData.setProdCode(prodCode);
            awardData.setProdName(prodName);
            awardData.setParentProdCode(prodCode + "-T");
            awardData.setParentProdName(prodName + "-推荐人奖励");
            awardData.setUserId(userId);
            awardData.setParentUserId(parentUserId);
            awardData.setParentPhone(parentPhone);
            awardData.setMsg("获取奖励数据成功");
            awardData.setUser(newUser);
            awardData.setDisMemberInfo(parentDisMemberInfo);
            awardData.setSettleList(userList.toJSONString());
        }
        return doDistProfitSplit(awardData, userId, parentCode, mchOrderNo, null);
    }

    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject makeDistProfitSplitByWxCommunity(String communityAdmin, String recommender, String userId, String amount, String amountUnit, int count) {
        String parentCode = "WXSQZC";
        String prodCode = "WXSQZC";
        String parentProdName;
        String prodName;
//        ResponseObject result;

        JSONObject data = new JSONObject();
        System.out.println("makeDistProfitSplitByWxCommunity->userId:" + userId);
        AwardDataModel awardData = new AwardDataModel();

        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("amount=" + amount);
        System.out.println("count=" + count);

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", userId);
        User newUser = userDataService.selectOne(wrapper1);
//        Wrapper<DisMemberInfoOen> wrapper = new EntityWrapper<>();
//        wrapper.eq("dis_user_id", userId);
//        DisMemberInfoOen disMemberInfo = disMemberInfoOenService.selectOne(wrapper);

//        if (StringUtils.isBlank(userId)) {
//            System.out.println("用户ID为空");
//            awardData.setCode("6");
//            awardData.setMsg("用户ID为空");
//        }
//
//        if (null == newUser) {
//            System.out.println("用户不存在");
//            awardData.setCode("2");
//            awardData.setMsg("用户不存在");
//            result = new ResponseObject();
//            result.setErroCode(2);
//            return result;
//        }

        ResponseObject result = new ResponseObject();
        Map map = new HashMap<>();
        if (StringUtils.isBlank(userId)) {
            System.out.println("用户ID为空");
            awardData.setCode("6");
            awardData.setMsg("用户ID为空");
            map.put("code", "6");
            map.put("msg", "用户ID为空");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            map.put("code", "2");
            map.put("msg", "用户不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        Goods goods = goodsSevice.selectByCode(prodCode);
        if(goods == null) {
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
        parentProdName = goods.getGoodsName();
        //parentProdName = parentProdName.replace("奖励", "");
        if(StringUtils.isBlank(parentProdName)) {
            System.out.println(parentProdName + "奖励在系统中不存在");
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer bibi;//bibi
        Map<String,Integer> re = (Map<String,Integer>) userDataService.fraction(parentCode, Double.valueOf(inputAmount));
        member=re.get("member");//huiyuang

        Integer amountInt = member;
        String total = Integer.toString(amountInt);

        System.out.println("普通会员：" + member);

//        Goods goods = goodsSevice.selectByCode(parentCode);
//        parentProdName = goods.getGoodsName();
//        if(StringUtils.isBlank(parentProdName)) {
//            System.out.println(parentProdName + "奖励在系统中不存在");
//            awardData.setCode("4");
//            awardData.setMsg("该奖励在系统中不存在");
//        }
        JSONArray userList = new JSONArray();

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", parentProdName + ":" + prodCode);
        userList.add(user);
        data.put("userId", member);
        data.put("selfUserId", userId);
        String prefit = "";
        String phone;

        User parentUser;
        if(StringUtils.isNotBlank(recommender)) {
            Wrapper<User> wrapperuser = new EntityWrapper<User>();
            wrapperuser.eq("user_id", recommender);
            newUser = userDataService.selectOne(wrapperuser);

            if(newUser != null) {
                System.out.println("合伙人上级推荐人：" + newUser.getName());
                {
                    //上级奖励
                    re = (Map<String,Integer>) userDataService.fraction("SQYQJL", Double.valueOf(inputAmount));
                    member=re.get("member");//huiyuang
                    amountInt += member;
                    total = Integer.toString(amountInt);
                    System.out.println("推荐人本人：" + member);
                    goods = goodsSevice.selectByCode("SQYQJL");
                    prodName = goods.getGoodsName();
                    if(StringUtils.isBlank(prodName)) {
                        System.out.println(prodName + "奖励在系统中不存在");
                        awardData.setCode("4");
                        awardData.setMsg("该奖励在系统中不存在");
                    }

                    phone = newUser.getPhone();
                    user = new JSONObject();
                    user.put("userId", recommender);
                    user.put("phone", phone);
                    user.put("amount", member);
                    user.put("comment", prodName + ":" + "SQYQJL");
                    userList.add(user);
                    data.put("recommender", member);
                    data.put("recommenderUserId", recommender);
                    //二级奖励
                    re = (Map<String,Integer>) userDataService.fraction("SQYQJLGLY", Double.valueOf(inputAmount));
                    member=re.get("member");//huiyuang
                    amountInt += member;
                    //String total = Double.toString(amountInt.doubleValue());
                    total = Integer.toString(amountInt);
                    System.out.println("上级推荐人本人：" + member);
                    goods = goodsSevice.selectByCode("SQYQJLGLY");
                    prodName = goods.getGoodsName();
                    if(StringUtils.isBlank(prodName)) {
                        System.out.println(prodName + "奖励在系统中不存在");
                        awardData.setCode("4");
                        awardData.setMsg("该奖励在系统中不存在");
                    }

                    if(StringUtils.isNotBlank(communityAdmin)) {
                        wrapperuser = new EntityWrapper<User>();
                        wrapperuser.eq("user_id", communityAdmin);
                        parentUser = userDataService.selectOne(wrapperuser);

                        if(parentUser != null) {
                            System.out.println("合伙人上级推荐人：" + parentUser.getName());
                            phone = parentUser.getPhone();
                            user = new JSONObject();
                            user.put("userId", communityAdmin);
                            user.put("phone", phone);
                            user.put("amount", member);
                            user.put("comment", prodName + ":" + "SQYQJLGLY");
                            userList.add(user);
                            data.put("communityAdmin", member);
                            data.put("communityAdminUserId", communityAdmin);
                        }
                    }else {
                        System.out.println("该用户无上级推荐人");
                    }
                }
            }
        }else {
            System.out.println("该用户无上级推荐人");
        }

        awardData.setOrderType("1");
        prefit = "奖励金额：";
        //设置为manual表示该笔奖励需要后续通过其他事件或者条件来触发到账，本次只是记账
        awardData.setUnfreezeStringTime("manual");
        awardData.setChannelId("HL_AWARD_HTL");//购买商品奖励
        awardData.setCurrency("htl");

        awardData.setOrderDetail(prefit + amount+" htl");//原始订单金额,单位：HTL

        awardData.setOrderAmount(total);
        awardData.setCode("0");
        awardData.setMemberAward(Integer.toString(member));
        awardData.setParentAward("0");
        awardData.setTotalAward(total);//总奖励数量
        awardData.setProdCode(parentCode);
        awardData.setProdName(parentProdName);
        awardData.setParentProdCode(null);
        awardData.setParentProdName(null);
        awardData.setUserId(userId);
        awardData.setParentUserId(null);
        awardData.setParentPhone(null);
        awardData.setMsg("获取奖励数据成功");
        awardData.setUser(newUser);
        awardData.setDisMemberInfo(null);
        awardData.setSettleList(userList.toJSONString());
//manual表示是异步到账，需要外部二次触发
        result = doDistProfitSplit(awardData, userId, parentCode, parentCode + userId, "manual");
        data.put("orderId", parentCode + userId);
        if(result.getErroCode() == 0) {
            //模拟手动触发到账
//            Map<String, String> map = new HashMap<String, String>();
//            //map.put("tripTime", saScoreFlow.getTripTime());
//            map.put("unfreeze", "manual");
//            map.put("userId", userId);
//            map.put("mchOrderNo", parentCode + userId);
//            map.put("isFree", "isFree");
//            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
            data.put("mchOrderNo", parentCode + userId);
            result.setData(data);
        }
        return result;
    }

    @Override
    public String confirmCommunityAward(String userId) {
        //模拟手动触发到账, 测试通过
        ZoneUserAward award = new ZoneUserAward();
        award.setUserId(userId);
        award = zoneUserAwardMapper.selectOne(award);
        if(award != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("unfreeze", "manual");
            map.put("userId", userId);
            map.put("mchOrderNo", "WXSQZC" + userId);
            map.put("amount", award.getUserIdAward().toString());
            map.put("isFree", "isFree");
            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());

            map = new HashMap<String, String>();
            map.put("unfreeze", "manual");
            map.put("userId", award.getRecommender());
            map.put("mchOrderNo", "WXSQZC" + userId);
            map.put("amount", award.getRecommenderAward().toString());
            map.put("isFree", "isFree");
            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());

            map = new HashMap<String, String>();
            map.put("unfreeze", "manual");
            map.put("userId", award.getCommunityAdmin());
            map.put("mchOrderNo", "WXSQZC" + userId);
            map.put("amount", award.getCommunityAdminAward().toString());
            map.put("isFree", "isFree");
            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
            System.out.println("补充激活社区注册奖励:" + award.getCommunityAdminAward().toString());
            return "补充激活发送成功";
        }
        return "订单不存在";
    }

    @Override
    public String confirmAuthAward(String orderId) {
        //模拟手动触发到账, 测试通过
        ZoneUserAwardThree award = new ZoneUserAwardThree();
        award.setOrderid(orderId);
        award = zoneUserAwardThreeMapper.selectOne(award);
        if (award != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("unfreeze", "manual");
            map.put("userId", award.getUserId());
            map.put("mchOrderNo", award.getOrderid());
            map.put("amount", award.getUserIdAward().toString());
            map.put("isFree", "isFree");
            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());

            if(StringUtils.isNotBlank(award.getRecommender())) {
                map = new HashMap<String, String>();
                map.put("unfreeze", "manual");
                map.put("userId", award.getRecommender());
                map.put("mchOrderNo", award.getOrderid());
                map.put("amount", award.getRecommenderAward().toString());
                map.put("isFree", "isFree");
                mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
            }

            if(StringUtils.isNotBlank(award.getCommunityAdmin())) {
                map = new HashMap<String, String>();
                map.put("unfreeze", "manual");
                map.put("userId", award.getCommunityAdmin());
                map.put("mchOrderNo", award.getOrderid());
                map.put("amount", award.getCommunityAdminAward().toString());
                map.put("isFree", "isFree");
                mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
            }
            System.out.println("激活推荐注册奖励:" + award.getCommunityAdminAward().toString());
            return "success";
        }
        return "order not found";
    }

    @Override
    public String manualUnfreezeAward(String userId, String mchOrderNo, String amount) {
        //模拟手动触发到账, 测试通过
        if(StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(mchOrderNo) && StringUtils.isNotBlank(amount)) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("unfreeze", "manual");
            map.put("userId", userId);
            map.put("mchOrderNo", mchOrderNo);
            map.put("amount", amount);
            map.put("isFree", "isFree");
            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
            return "manual unfreeze success";
        }
        return "fail";
    }

//    @Override
//    public String reScanConfirmCommunityAward() {
//        Wrapper<ZoneUserAward> wrapper=new EntityWrapper<ZoneUserAward>();
//        wrapper.eq("amount",0);
//        List<ZoneUserAward> awardList = zoneUserAwardMapper.selectList(wrapper);
//
//        User recommender = new User();
//        Wrapper<User> wrapper1=new EntityWrapper<>();
//        List<User> users;
//        User user;
//        wrapper1=new EntityWrapper<>();
//        wrapper1.eq("refer_reg_code", recommender.getReg_code());
//        users = userDataService.selectList(wrapper1);
//
//        //模拟手动触发到账, 测试通过
//        ZoneUserAward award = new ZoneUserAward();
//        award.setUserId(userId);
//        award = zoneUserAwardMapper.selectOne(award);
//        if(award != null) {
//            Map<String, String> map = new HashMap<String, String>();
//            map.put("unfreeze", "manual");
//            map.put("userId", userId);
//            map.put("mchOrderNo", "WXSQZC" + userId);
//            map.put("amount", award.getUserIdAward().toString());
//            map.put("isFree", "isFree");
//            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
//
//            map = new HashMap<String, String>();
//            map.put("unfreeze", "manual");
//            map.put("userId", award.getRecommender());
//            map.put("mchOrderNo", "WXSQZC" + userId);
//            map.put("amount", award.getRecommenderAward().toString());
//            map.put("isFree", "isFree");
//            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
//
//            map = new HashMap<String, String>();
//            map.put("unfreeze", "manual");
//            map.put("userId", award.getCommunityAdmin());
//            map.put("mchOrderNo", "WXSQZC" + userId);
//            map.put("amount", award.getCommunityAdminAward().toString());
//            map.put("isFree", "isFree");
//            mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
//            System.out.println("补充激活社区注册奖励:" + award.getCommunityAdminAward().toString());
//            return "补充激活发送成功";
//        }
//        return "订单不存在";
//    }

    @Override
    public ResponseObject makeCommunityAward(String parentCode, String userId, String amount, String orderId) {
        String prodCode = parentCode;
        String parentProdName;
        String prodName;
        //ResponseObject result;

        JSONObject data = new JSONObject();
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + parentCode);
        AwardDataModel awardData = new AwardDataModel();

        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("amount=" + amount);

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", userId);
        User newUser = userDataService.selectOne(wrapper1);

//        if (StringUtils.isBlank(userId)) {
//            System.out.println("用户ID为空");
//            awardData.setCode("6");
//            awardData.setMsg("用户ID为空");
//        }
//
//        if (null == newUser) {
//            System.out.println("用户不存在");
//            awardData.setCode("2");
//            awardData.setMsg("用户不存在");
//        }
        ResponseObject result = new ResponseObject();
        Map map = new HashMap<>();
        if (StringUtils.isBlank(userId)) {
            System.out.println("用户ID为空");
            awardData.setCode("6");
            awardData.setMsg("用户ID为空");
            map.put("code", "6");
            map.put("msg", "用户ID为空");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            map.put("code", "2");
            map.put("msg", "用户不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        Goods goods = goodsSevice.selectByCode(prodCode);
        if(goods == null) {
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
        parentProdName = goods.getGoodsName();
        //parentProdName = parentProdName.replace("奖励", "");
        if(StringUtils.isBlank(parentProdName)) {
            System.out.println(parentProdName + "奖励在系统中不存在");
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer plantForm;//plantForm
        Map<String,Object> re = (Map<String,Object>) userDataService.fractionV1(parentCode, Double.valueOf(inputAmount), userId);
        member= (Integer) re.get("member");//huiyuang
        partner= (Integer) re.get("partner");//huiyuang
        plantForm= (Integer) re.get("plantForm");//huiyuang
        Integer amountInt;

        System.out.println("普通会员：" + member);

//        Goods goods = goodsSevice.selectByCode(parentCode);
//        parentProdName = goods.getGoodsName();
//        //parentProdName = parentProdName.replace("奖励", "");
//        if(StringUtils.isBlank(parentProdName)) {
//            System.out.println(parentProdName + "奖励在系统中不存在");
//            awardData.setCode("4");
//            awardData.setMsg("该奖励在系统中不存在");
//        }
        JSONArray userList = new JSONArray();

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", parentProdName + "-本人" + ":" + prodCode);
        userList.add(user);
        data.put("userId", member);
        data.put("selfUserId", userId);
        amountInt = member;
        wrapper1 = new EntityWrapper<>();
        wrapper1.eq("reg_code", newUser.getRefer_reg_code());
        User partnerUser = userDataService.selectOne(wrapper1);
        if(partnerUser != null && partner != 0) {
            JSONObject partnerObj = new JSONObject();
            partnerObj.put("userId", partnerUser.getUserId());
            partnerObj.put("phone", partnerUser.getPhone());
            partnerObj.put("amount", partner);
            partnerObj.put("comment", parentProdName + "-推荐人" + ":" + prodCode);
            userList.add(partnerObj);
            data.put("recommender", partner);
            data.put("recommenderUserId", partnerUser.getUserId());
            amountInt = member + partner;
            wrapper1 = new EntityWrapper<>();
            wrapper1.eq("reg_code", partnerUser.getRefer_reg_code());
            User plantFormUser = userDataService.selectOne(wrapper1);
            if(plantFormUser != null && plantForm != 0) {
                JSONObject plantformObj = new JSONObject();
                plantformObj.put("userId", plantFormUser.getUserId());
                plantformObj.put("phone", plantFormUser.getPhone());
                plantformObj.put("amount", plantForm);
                plantformObj.put("comment", parentProdName + "-社区管理员" + ":" + prodCode);
                userList.add(plantformObj);
                data.put("communityAdmin", plantForm);
                data.put("communityAdminUserId", plantFormUser.getUserId());
                amountInt = member + partner + plantForm;
            }
        }
        String total = Integer.toString(amountInt);
        String prefit = "奖励金额：";
        String currency = (String) re.get("currency");
        awardData.setCurrency(currency);
        awardData.setOrderType("1");
        awardData.setUnfreezeStringTime(null);
        data.put("currency", currency.toUpperCase());
        if("htl".equals(currency)) {
            awardData.setChannelId("HL_AWARD_HTL");//购买商品或其他高级奖励
        }else {
            awardData.setChannelId("HL_AWARD");//普通奖励
        }

        awardData.setOrderDetail(prefit + total+currency);//原始订单金额,单位：HTL

        awardData.setOrderAmount(total);
        awardData.setCode("0");
        awardData.setMemberAward(Integer.toString(member));
        awardData.setParentAward("0");
        awardData.setTotalAward(total);//总奖励数量
        awardData.setProdCode(parentCode);
        awardData.setProdName(parentProdName);
        awardData.setParentProdCode(null);
        awardData.setParentProdName(null);
        awardData.setUserId(userId);
        awardData.setParentUserId(null);
        awardData.setParentPhone(null);
        awardData.setMsg("获取奖励数据成功");
        awardData.setUser(newUser);
        awardData.setDisMemberInfo(null);
        awardData.setSettleList(userList.toJSONString());

        result = doDistProfitSplit(awardData, userId, parentCode, orderId, null);
        if(result.getErroCode() == 0) {
            data.put("mchOrderNo", orderId);
            result.setData(data);
        }
        return result;
    }

    @Override
    public ResponseObject makeAwardByCount(String parentCode, String userId, String amount, String orderId, int count) {
        return makeAwardByCount(parentCode, userId, amount, orderId, count, null);
    }

    private ResponseObject makeAwardByCount(String parentCode, String userId, String amount, String orderId, int count, String unfreezeTime) {
        String prodCode = parentCode;
        String parentProdName;
        String prodName;
        //ResponseObject result;

        JSONObject data = new JSONObject();
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + parentCode);
        AwardDataModel awardData = new AwardDataModel();

        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("amount=" + amount);

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", userId);
        User newUser = userDataService.selectOne(wrapper1);

//        if (StringUtils.isBlank(userId)) {
//            System.out.println("用户ID为空");
//            awardData.setCode("6");
//            awardData.setMsg("用户ID为空");
//        }
//
//        if (null == newUser) {
//            System.out.println("用户不存在");
//            awardData.setCode("2");
//            awardData.setMsg("用户不存在");
//        }
        ResponseObject result = new ResponseObject();
        Map map = new HashMap<>();
        if (StringUtils.isBlank(userId)) {
            System.out.println("用户ID为空");
            awardData.setCode("6");
            awardData.setMsg("用户ID为空");
            map.put("code", "6");
            map.put("msg", "用户ID为空");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            map.put("code", "2");
            map.put("msg", "用户不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        Goods goods = goodsSevice.selectByCode(prodCode);
        if(goods == null) {
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
        parentProdName = goods.getGoodsName();
        //parentProdName = parentProdName.replace("奖励", "");
        if(StringUtils.isBlank(parentProdName)) {
            System.out.println(parentProdName + "奖励在系统中不存在");
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer plantForm;//plantForm
        Map<String,Object> re = (Map<String,Object>) userDataService.fractionV1(parentCode, Double.valueOf(inputAmount), userId);
        member= (Integer) re.get("member");//huiyuang
        partner= (Integer) re.get("partner");//huiyuang
        plantForm= (Integer) re.get("plantForm");//huiyuang
        if(count > 0) {
            member= member*count;//huiyuang
            partner= partner*count;//huiyuang
            plantForm= plantForm*count;//huiyuang
        }
        Integer amountInt;

        System.out.println("普通会员：" + member);

//        Goods goods = goodsSevice.selectByCode(parentCode);
//        parentProdName = goods.getGoodsName();
//        //parentProdName = parentProdName.replace("奖励", "");
//        if(StringUtils.isBlank(parentProdName)) {
//            System.out.println(parentProdName + "奖励在系统中不存在");
//            awardData.setCode("4");
//            awardData.setMsg("该奖励在系统中不存在");
//        }
        JSONArray userList = new JSONArray();

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", parentProdName + "-本人" + ":" + prodCode);
        userList.add(user);
        data.put("userId", member);
        data.put("selfUserId", userId);
        amountInt = member;
        wrapper1 = new EntityWrapper<>();
        wrapper1.eq("reg_code", newUser.getRefer_reg_code());
        User partnerUser = userDataService.selectOne(wrapper1);
        if(partnerUser != null && partner != 0) {
            JSONObject partnerObj = new JSONObject();
            partnerObj.put("userId", partnerUser.getUserId());
            partnerObj.put("phone", partnerUser.getPhone());
            partnerObj.put("amount", partner);
            partnerObj.put("comment", parentProdName + "-推荐人" + ":" + prodCode);
            userList.add(partnerObj);
            data.put("recommender", partner);
            data.put("recommenderUserId", partnerUser.getUserId());
            amountInt = member + partner;
            wrapper1 = new EntityWrapper<>();
            wrapper1.eq("reg_code", partnerUser.getRefer_reg_code());
            User plantFormUser = userDataService.selectOne(wrapper1);
            if(plantFormUser != null && plantForm != 0) {
                JSONObject plantformObj = new JSONObject();
                plantformObj.put("userId", plantFormUser.getUserId());
                plantformObj.put("phone", plantFormUser.getPhone());
                plantformObj.put("amount", plantForm);
                plantformObj.put("comment", parentProdName + "-推荐人上级" + ":" + prodCode);
                userList.add(plantformObj);
                data.put("communityAdmin", plantForm);
                data.put("communityAdminUserId", plantFormUser.getUserId());
                amountInt = member + partner + plantForm;
            }
        }
        String total = Integer.toString(amountInt);
        String prefit = "奖励金额：";
        String currency = (String) re.get("currency");
        awardData.setCurrency(currency);
        awardData.setOrderType("1");
        awardData.setUnfreezeStringTime(null);
        data.put("currency", currency.toUpperCase());
        if("htl".equals(currency)) {
            awardData.setChannelId("HL_AWARD_HTL");//购买商品或其他高级奖励
        }else {
            awardData.setChannelId("HL_AWARD");//普通奖励
        }

        awardData.setOrderDetail(prefit + total+currency);//原始订单金额,单位：HTL

        awardData.setOrderAmount(total);
        awardData.setCode("0");
        awardData.setMemberAward(Integer.toString(member));
        awardData.setParentAward("0");
        awardData.setTotalAward(total);//总奖励数量
        awardData.setProdCode(parentCode);
        awardData.setProdName(parentProdName);
        awardData.setParentProdCode(null);
        awardData.setParentProdName(null);
        awardData.setUserId(userId);
        awardData.setParentUserId(null);
        awardData.setParentPhone(null);
        awardData.setMsg("获取奖励数据成功");
        awardData.setUser(newUser);
        awardData.setDisMemberInfo(null);
        awardData.setSettleList(userList.toJSONString());

        result = doDistProfitSplit(awardData, userId, parentCode, orderId, unfreezeTime);
        if(result.getErroCode() == 0) {
            data.put("mchOrderNo", orderId);
            result.setData(data);
        }
        return result;
    }

    @Override
    public ResponseObject makeAward(String parentCode, String userId, String amount, String orderId) {
        return makeAward(parentCode, userId, amount, orderId, null);
    }

    @Override
    public ResponseObject makeAward(String parentCode, String userId, String amount, String orderId, String unfreezeTime) {
        String prodCode = parentCode;
        String parentProdName;
        String prodName;
//        ResponseObject result;

        JSONObject data = new JSONObject();
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + parentCode);
        AwardDataModel awardData = new AwardDataModel();

        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("amount=" + amount);

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", userId);
        User newUser = userDataService.selectOne(wrapper1);

//        if (StringUtils.isBlank(userId)) {
//            System.out.println("用户ID为空");
//            awardData.setCode("6");
//            awardData.setMsg("用户ID为空");
//        }
//
//        if (null == newUser) {
//            System.out.println("用户不存在");
//            awardData.setCode("2");
//            awardData.setMsg("用户不存在");
//        }

        ResponseObject result = new ResponseObject();
        Map map = new HashMap<>();
        if (StringUtils.isBlank(userId)) {
            System.out.println("用户ID为空");
            awardData.setCode("6");
            awardData.setMsg("用户ID为空");
            map.put("code", "6");
            map.put("msg", "用户ID为空");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            map.put("code", "2");
            map.put("msg", "用户不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        Goods goods = goodsSevice.selectByCode(prodCode);
        if(goods == null) {
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
        parentProdName = goods.getGoodsName();
        //parentProdName = parentProdName.replace("奖励", "");
        if(StringUtils.isBlank(parentProdName)) {
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer plantForm;//plantForm
        Map<String,Object> re = (Map<String,Object>) userDataService.fractionV1(parentCode, Double.valueOf(inputAmount), userId);
        member= (Integer) re.get("member");//huiyuang
        partner= (Integer) re.get("partner");//huiyuang
        plantForm= (Integer) re.get("plantForm");//huiyuang
        Integer amountInt;

        System.out.println("普通会员：" + member);

//        Goods goods = goodsSevice.selectByCode(parentCode);
//        parentProdName = goods.getGoodsName();
//        //parentProdName = parentProdName.replace("奖励", "");
//        if(StringUtils.isBlank(parentProdName)) {
//            System.out.println(parentProdName + "奖励在系统中不存在");
//            awardData.setCode("4");
//            awardData.setMsg("该奖励在系统中不存在");
//        }
        JSONArray userList = new JSONArray();

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", parentProdName + "-本人" + ":" + prodCode);
        userList.add(user);
        data.put("userId", member);
        data.put("selfUserId", userId);
        amountInt = member;
        wrapper1 = new EntityWrapper<>();
        wrapper1.eq("reg_code", newUser.getRefer_reg_code());
        User partnerUser = userDataService.selectOne(wrapper1);
        if(partnerUser != null && partner != 0) {
            JSONObject partnerObj = new JSONObject();
            partnerObj.put("userId", partnerUser.getUserId());
            partnerObj.put("phone", partnerUser.getPhone());
            partnerObj.put("amount", partner);
            partnerObj.put("comment", parentProdName + "-推荐人" + ":" + prodCode);
            userList.add(partnerObj);
            data.put("recommender", partner);
            data.put("recommenderUserId", partnerUser.getUserId());
            amountInt = member + partner;
            wrapper1 = new EntityWrapper<>();
            wrapper1.eq("reg_code", partnerUser.getRefer_reg_code());
            User plantFormUser = userDataService.selectOne(wrapper1);
            if(plantFormUser != null && plantForm != 0) {
                JSONObject plantformObj = new JSONObject();
                plantformObj.put("userId", plantFormUser.getUserId());
                plantformObj.put("phone", plantFormUser.getPhone());
                plantformObj.put("amount", plantForm);
                plantformObj.put("comment", parentProdName + "-推荐人上级" + ":" + prodCode);
                userList.add(plantformObj);
                data.put("communityAdmin", plantForm);
                data.put("communityAdminUserId", plantFormUser.getUserId());
                amountInt = member + partner + plantForm;
            }
        }
        String total = Integer.toString(amountInt);
        String prefit = "奖励金额：";
        String currency = (String) re.get("currency");
        awardData.setCurrency(currency);
        awardData.setOrderType("1");
        awardData.setUnfreezeStringTime(null);
        data.put("currency", currency.toUpperCase());
        if("htl".equals(currency)) {
            awardData.setChannelId("HL_AWARD_HTL");//购买商品或其他高级奖励
        }else {
            awardData.setChannelId("HL_AWARD");//普通奖励
        }

        awardData.setOrderDetail(prefit + total+currency);//原始订单金额,单位：HTL

        awardData.setOrderAmount(total);
        awardData.setCode("0");
        awardData.setMemberAward(Integer.toString(member));
        awardData.setParentAward("0");
        awardData.setTotalAward(total);//总奖励数量
        awardData.setProdCode(parentCode);
        awardData.setProdName(parentProdName);
        awardData.setParentProdCode(null);
        awardData.setParentProdName(null);
        awardData.setUserId(userId);
        awardData.setParentUserId(null);
        awardData.setParentPhone(null);
        awardData.setMsg("获取奖励数据成功");
        awardData.setUser(newUser);
        awardData.setDisMemberInfo(null);
        awardData.setSettleList(userList.toJSONString());

        result = doDistProfitSplit(awardData, userId, parentCode, orderId, unfreezeTime);
        if(result.getErroCode() == 0) {
            data.put("mchOrderNo", orderId);
            result.setData(data);
        }
        return result;
    }

    @Override
    public ResponseObject makeAdvanceAward(String parentCode, String regCode, String userId, String amount, String orderId) {
        return makeAdvanceAward(parentCode, regCode, userId, amount, orderId, null);
    }
    @Override
    public ResponseObject makeAdvanceAward(String parentCode, String regCode, String userId, String amount, String orderId, String unfreezeTime) {
        String prodCode = parentCode;
        String parentProdName;
        String prodName;

        JSONObject data = new JSONObject();
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + parentCode);
        AwardDataModel awardData = new AwardDataModel();

        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("amount=" + amount);

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", userId);
        User newUser = userDataService.selectOne(wrapper1);

//        if (StringUtils.isBlank(userId)) {
//            System.out.println("用户ID为空");
//            awardData.setCode("6");
//            awardData.setMsg("用户ID为空");
//        }
//
//        if (null == newUser) {
//            System.out.println("用户不存在");
//            awardData.setCode("2");
//            awardData.setMsg("用户不存在");
//        }

        ResponseObject result = new ResponseObject();
        Map map = new HashMap<>();
        if (StringUtils.isBlank(userId)) {
            System.out.println("用户ID为空");
            awardData.setCode("6");
            awardData.setMsg("用户ID为空");
            map.put("code", "6");
            map.put("msg", "用户ID为空");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            map.put("code", "2");
            map.put("msg", "用户不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        Goods goods = goodsSevice.selectByCode(prodCode);
        if(goods == null) {
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
        parentProdName = goods.getGoodsName();
        //parentProdName = parentProdName.replace("奖励", "");
        if(StringUtils.isBlank(parentProdName)) {
            System.out.println(parentProdName + "奖励在系统中不存在");
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer plantForm;//plantForm
        Map<String,Object> re = (Map<String,Object>) userDataService.fractionV1(parentCode, Double.valueOf(inputAmount), userId);
        member= (Integer) re.get("member");//huiyuang
        partner= (Integer) re.get("partner");//huiyuang
        plantForm= (Integer) re.get("plantForm");//huiyuang
        Integer amountInt = member;

        System.out.println("普通会员：" + member);

//        Goods goods = goodsSevice.selectByCode(parentCode);
//        parentProdName = goods.getGoodsName();
//        //parentProdName = parentProdName.replace("奖励", "");
//        if(StringUtils.isBlank(parentProdName)) {
//            System.out.println(parentProdName + "奖励在系统中不存在");
//            awardData.setCode("4");
//            awardData.setMsg("该奖励在系统中不存在");
//        }
        JSONArray userList = new JSONArray();

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", parentProdName + "-本人" + ":" + prodCode);
        userList.add(user);
        data.put("userId", member);
        data.put("selfUserId", userId);
        wrapper1 = new EntityWrapper<>();
        wrapper1.eq("reg_code", newUser.getRefer_reg_code());
        User partnerUser = userDataService.selectOne(wrapper1);
        if(partnerUser != null && partner != 0) {
            JSONObject partnerObj = new JSONObject();
            partnerObj.put("userId", partnerUser.getUserId());
            partnerObj.put("phone", partnerUser.getPhone());
            partnerObj.put("amount", partner);
            partnerObj.put("comment", parentProdName + "-推荐人" + ":" + prodCode);
            userList.add(partnerObj);
            data.put("recommender", partner);
            data.put("recommenderUserId", partnerUser.getUserId());
            amountInt = member + partner;
            wrapper1 = new EntityWrapper<>();
            wrapper1.eq("reg_code", partnerUser.getRefer_reg_code());
            User plantFormUser = userDataService.selectOne(wrapper1);
            if(plantFormUser != null && plantForm != 0) {
                JSONObject plantformObj = new JSONObject();
                plantformObj.put("userId", plantFormUser.getUserId());
                plantformObj.put("phone", plantFormUser.getPhone());
                plantformObj.put("amount", plantForm);
                plantformObj.put("comment", parentProdName + "-推荐人上级" + ":" + prodCode);
                userList.add(plantformObj);
                data.put("communityAdmin", plantForm);
                data.put("communityAdminUserId", plantFormUser.getUserId());
                amountInt = member + partner + plantForm;
            }
        }
        //对直接推荐人进行奖励
        int directRecommonderAwardAmount = 0;
        if(StringUtils.isNotBlank(regCode)) {
            wrapper1 = new EntityWrapper<>();
            wrapper1.eq("reg_code", regCode);
            User refUser = userDataService.selectOne(wrapper1);
            if(refUser != null) {
                String subCode = goods.getNowtype();
                if (StringUtils.isNotBlank(subCode)) {
                    directRecommonderAwardAmount = makeSubAward(subCode, regCode, amount, userList, data);
                }
            }
        }
        String total = Integer.toString(amountInt + directRecommonderAwardAmount);
        String prefit = "奖励金额：";
        String currency = (String) re.get("currency");
        awardData.setCurrency(currency);
        awardData.setOrderType("1");
        awardData.setUnfreezeStringTime(null);
        data.put("currency", currency.toUpperCase());
        if("htl".equals(currency)) {
            awardData.setChannelId("HL_AWARD_HTL");//购买商品或其他高级奖励
        }else {
            awardData.setChannelId("HL_AWARD");//普通奖励
        }

        awardData.setOrderDetail(prefit + total+currency);//原始订单金额,单位：HTL

        awardData.setOrderAmount(total);
        awardData.setCode("0");
        awardData.setMemberAward(Integer.toString(member));
        awardData.setParentAward("0");
        awardData.setTotalAward(total);//总奖励数量
        awardData.setProdCode(parentCode);
        awardData.setProdName(parentProdName);
        awardData.setParentProdCode(null);
        awardData.setParentProdName(null);
        awardData.setUserId(userId);
        awardData.setParentUserId(null);
        awardData.setParentPhone(null);
        awardData.setMsg("获取奖励数据成功");
        awardData.setUser(newUser);
        awardData.setDisMemberInfo(null);
        awardData.setSettleList(userList.toJSONString());

        result = doDistProfitSplit(awardData, userId, parentCode, orderId, unfreezeTime);
        if(result.getErroCode() == 0) {
            data.put("mchOrderNo", orderId);
            result.setData(data);
        }
        return result;
    }

    public int makeSubAward(String parentCode, String regCode, String amount, JSONArray userList, JSONObject data) {
        String prodCode = parentCode;
        String parentProdName;

        AwardDataModel awardData = new AwardDataModel();

        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("amount=" + amount);

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("reg_code", regCode);
        User newUser = userDataService.selectOne(wrapper1);

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            return 0;
        }

        String userId = newUser.getUserId();
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer plantForm;//plantForm
        Map<String,Object> re = (Map<String,Object>) userDataService.fractionV1(parentCode, Double.valueOf(inputAmount), userId);
        member= (Integer) re.get("member");//huiyuang
        partner= (Integer) re.get("partner");//huiyuang
        plantForm= (Integer) re.get("plantForm");//huiyuang
        Integer amountInt;

        System.out.println("普通会员：" + member);

        Goods goods = goodsSevice.selectByCode(parentCode);
        parentProdName = goods.getGoodsName();
        //parentProdName = parentProdName.replace("奖励", "");
        if(StringUtils.isBlank(parentProdName)) {
            System.out.println(parentProdName + "奖励在系统中不存在");
            awardData.setCode("4");
            awardData.setMsg("该奖励在系统中不存在");
            return 0;
        }

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", parentProdName + "-本人" + ":" + prodCode);
        userList.add(user);
        data.put("directRecommender", member);
        amountInt = member;
        wrapper1 = new EntityWrapper<>();
        wrapper1.eq("reg_code", newUser.getRefer_reg_code());
        User partnerUser = userDataService.selectOne(wrapper1);
        if(partnerUser != null && partner != 0) {
            JSONObject partnerObj = new JSONObject();
            partnerObj.put("userId", partnerUser.getUserId());
            partnerObj.put("phone", partnerUser.getPhone());
            partnerObj.put("amount", partner);
            partnerObj.put("comment", parentProdName + "-推荐人" + ":" + prodCode);
            userList.add(partnerObj);
            data.put("directParentRecommender", partner);
            amountInt = member + partner;
            wrapper1 = new EntityWrapper<>();
            wrapper1.eq("reg_code", partnerUser.getRefer_reg_code());
            User plantFormUser = userDataService.selectOne(wrapper1);
            if(plantFormUser != null && plantForm != 0) {
                JSONObject plantformObj = new JSONObject();
                plantformObj.put("userId", plantFormUser.getUserId());
                plantformObj.put("phone", plantFormUser.getPhone());
                plantformObj.put("amount", plantForm);
                plantformObj.put("comment", parentProdName + "-推荐人上级" + ":" + prodCode);
                userList.add(plantformObj);
                data.put("directCommunityAdmin", plantForm);
                amountInt = member + partner + plantForm;
            }
        }
        return amountInt;
    }

    @Override
    public ResponseObject makeDistProfit(String prodCode, String orderId, String parentRecommender, String recommender, String userId, String amount, String unfreezeTime) {
        return makeDistProfit(prodCode, orderId, parentRecommender, recommender, userId, amount, 1, unfreezeTime);
    }
    /**
     * 方法实现说明
     * @method
     * @author      Pullwind
     * @version
     * @see
     * @Param:
     * @return
     * @exception
     * @date        2018/7/7 13:57
     */
    public ResponseObject makeDistProfit(String prodCode, String orderId, String parentRecommender, String recommender, String userId, String amount, int count, String unfreezeTime) {
        String parentProdName;
        String prodName;

        JSONObject data = new JSONObject();
        System.out.println("makeDistProfitSplitByTrans->parentCode:" + prodCode);
        AwardDataModel awardData = new AwardDataModel();

        //User newUser = userDataService.selectById(user.getUserId());
        Long inputAmount = Long.parseLong(amount);
        int inputAmountInt = Integer.parseInt(amount);
        System.out.println("amount=" + amount);

        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", userId);
        User newUser = userDataService.selectOne(wrapper1);

        ResponseObject result = new ResponseObject();
        Map map = new HashMap<>();
        if (StringUtils.isBlank(userId)) {
            System.out.println("用户ID为空");
            awardData.setCode("6");
            awardData.setMsg("用户ID为空");
            map.put("code", "6");
            map.put("msg", "用户ID为空");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        if (null == newUser) {
            System.out.println("用户不存在");
            awardData.setCode("2");
            awardData.setMsg("用户不存在");
            map.put("code", "2");
            map.put("msg", "用户不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }

        Goods goods = goodsSevice.selectByCode(prodCode);
        if(goods == null) {
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
        parentProdName = goods.getGoodsName();
        //parentProdName = parentProdName.replace("奖励", "");
        if(StringUtils.isBlank(parentProdName)) {
            System.out.println(parentProdName + "奖励在系统中不存在");
            map.put("code", "4");
            map.put("msg", "该奖励在系统中不存在");
            result.setData(map);
            result.setErroCode(1);
            return result;
        }
//		String mchOrderNo;
//		if(repeate) {
//			mchOrderNo = prodCode + System.currentTimeMillis();
//		}else {
//			mchOrderNo = prodCode + userId;
//		}
        Integer member;//huiyuang
        Integer partner;//hehuoren
        Integer plantForm;//plantForm
        Map<String,Object> re = (Map<String,Object>) userDataService.fractionV1(prodCode, Double.valueOf(inputAmount), userId);
        if(count > 1) {
            member = (Integer) re.get("member") * count;//huiyuang
            partner = (Integer) re.get("partner") * count;//huiyuang
            plantForm = (Integer) re.get("plantForm") * count;//huiyuang
        }else {
            member = (Integer) re.get("member");//huiyuang
            partner = (Integer) re.get("partner");//huiyuang
            plantForm = (Integer) re.get("plantForm");//huiyuang
        }
        Integer amountInt;

        System.out.println("普通会员：" + member);

        JSONArray userList = new JSONArray();

        JSONObject user = new JSONObject();
        user.put("userId", userId);
        user.put("phone", newUser.getPhone());
        user.put("amount", member);
        user.put("comment", parentProdName + "-本人" + ":" + prodCode);
        userList.add(user);
        data.put("userId", member);
        data.put("selfUserId", userId);
        amountInt = member;
        wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", recommender);
        User partnerUser = userDataService.selectOne(wrapper1);
        if(partnerUser != null && partner != 0) {
            JSONObject partnerObj = new JSONObject();
            partnerObj.put("userId", partnerUser.getUserId());
            partnerObj.put("phone", partnerUser.getPhone());
            partnerObj.put("amount", partner);
            partnerObj.put("comment", parentProdName + "-推荐人" + ":" + prodCode + ":T");
            userList.add(partnerObj);
            data.put("recommender", partner);
            data.put("recommenderUserId", partnerUser.getUserId());
            amountInt = member + partner;
            wrapper1 = new EntityWrapper<>();
            wrapper1.eq("user_id", parentRecommender);
            User plantFormUser = userDataService.selectOne(wrapper1);
            if(plantFormUser != null && plantForm != 0) {
                JSONObject plantformObj = new JSONObject();
                plantformObj.put("userId", plantFormUser.getUserId());
                plantformObj.put("phone", plantFormUser.getPhone());
                plantformObj.put("amount", plantForm);
                plantformObj.put("comment", parentProdName + "-推荐人上级" + ":" + prodCode + ":PT");
                userList.add(plantformObj);
                data.put("communityAdmin", plantForm);
                data.put("communityAdminUserId", plantFormUser.getUserId());
                amountInt = member + partner + plantForm;
            }
        }
        String total = Integer.toString(amountInt);
        String prefit = "奖励金额：";
        String currency = (String) re.get("currency");
        awardData.setCurrency(currency);
        awardData.setOrderType("1");
        awardData.setUnfreezeStringTime(null);
        data.put("currency", currency.toUpperCase());
        if("htl".equals(currency)) {
            awardData.setChannelId("HL_AWARD_HTL");//购买商品或其他高级奖励
        }else {
            awardData.setChannelId("HL_AWARD");//普通奖励
        }

        awardData.setOrderDetail(prefit + total+currency);//原始订单金额,单位：HTL

        awardData.setOrderAmount(total);
        awardData.setCode("0");
        awardData.setMemberAward(Integer.toString(member));
        awardData.setParentAward("0");
        awardData.setTotalAward(total);//总奖励数量
        awardData.setProdCode(prodCode);
        awardData.setProdName(parentProdName);
        awardData.setParentProdCode(null);
        awardData.setParentProdName(null);
        awardData.setUserId(userId);
        awardData.setParentUserId(null);
        awardData.setParentPhone(null);
        awardData.setMsg("获取奖励数据成功");
        awardData.setUser(newUser);
        awardData.setDisMemberInfo(null);
        awardData.setSettleList(userList.toJSONString());

        result = doDistProfitSplit(awardData, userId, prodCode, orderId, unfreezeTime);
        if(result.getErroCode() == 0) {
            data.put("mchOrderNo", orderId);
            result.setData(data);
        }
        return result;
    }
}
